<?php

namespace app\servercertificate\service\certificate;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-06-16 16:27:48
 * @Description: 证书转让
 */

class TransferLog extends ApiBase
{
	
	protected $transferLogLogic = 'app\servercertificate\logic\TransferLog';
	protected $certificateLogic = 'app\servercertificate\logic\Certificate';
    protected $transferLogValidate = 'app\servercertificate\validate\TransferLog';
    protected $addIssetFiels = [];

    /*证书转让*/
    // 版本2 此方法需要再改一下
    public function transfer(): array 
    {
    	$data = $this->haveIssetReceiveData(['certificate_id'=>0,'user_id'=>0,'to_user'=>0,'user_info'=>[],'to_user_info'=>[]]);

    	$check = $this->_checkTransfer($data);  //返回证书信息
    	if($check['status'] != SUCCESS_STATUS){
    		return $check;
    	}

        $data['certificate_info'] = $check['res'];
    	return $this->logic($this->transferLogLogic)->transfer($data);
    }

    /*艺货 - 证书退款 用户将证书直接转让给平台*/
    // 后来艺货版本2中需要加 goods_id 和 certificate_type
    public function transferDirect(): array 
    {
        $data = $this->haveIssetReceiveData(['certificate_id'=>0,'user_id'=>0,'to_user'=>0,'user_info'=>[],'to_user_info'=>[],'type'=>'','certificate_price'=>0,'goods_id'=>0,'certificate_type'=>'']);
        return $this->stepRun($data, 'transferDirect', 'transferDirect');
    }

    /*
    	证书转让时进行的验证
    	传入的数据 转让人是不是证书持有人 证书的存在性 该证书是否在转让中
    */
    protected function _checkTransfer($data): array 
    {

        $validate = validate($this->transferLogValidate);
  
        if( !$validate->scene('transfer')->check($data) ){ return verify_err(null, $validate->getError()); }
        if( !$validate->scene('checkUserInfo')->check($data['user_info']) ){ return verify_err(null, $validate->getError()); }  
        if( !$validate->scene('checkUserInfo')->check($data['to_user_info']) ){ return verify_err(null, $validate->getError()); }

        $info = logic($this->certificateLogic)->dbCertifiInfo(['id'=>$data['certificate_id'],'user_id'=>$data['user_id'],'status'=>['1']]);
        if(!$info){
        	return verify_err(null, '该证书不存在或未审核通过或该证书不属于您');
        }
        if($info['is_transfer']){
            return verify_err(null, '该证书正在转让中,不可再次转让');
        }

        return auto_success($info, '该证书可以进行该次转让');
      
    }

    /*对方同意证书的转让*/
    public function agreeTransfer(): array 
    {
        $data = $this->receiveData(['certificate_id'=>0,'to_user'=>0]);
        $validate = validate($this->transferLogValidate);
        if( !$validate->check($data) ){ return verify_err(null, $validate->getError()); }

        return $this->logic($this->transferLogLogic)->replyTransfer($data, true);
    }

    /*对方拒绝证书的转让*/
    public function refuseTransfer(): array 
    {
        $data = $this->receiveData(['certificate_id'=>0,'to_user'=>0]);
        $validate = validate($this->transferLogValidate);
        if( !$validate->check($data) ){ return verify_err(null, $validate->getError()); }

        return $this->logic($this->transferLogLogic)->replyTransfer($data, false);
    }

    /*
        注： 此方法为获取    “我未处理的证书列表” 或 “我已转让成功的证书列表”
        当user_id存在值时为获取 我已转让成功的证书列表(另外限制：此证书已不属于我 获取到的证书没有重复的)
        当to_user存在值时为获取 我未处理的证书列表
    */
    public function transferCertiList(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'to_user'=>0, 'page'=>1, 'listRows'=>4, 'type'=>' '], []);
        if($data['user_id'] == 0 && $data['to_user'] == 0){
            return verify_err(null, 'user_id和to_user不可同时为0');
        }
        if($data['user_id'] && $data['to_user']){
            return verify_err(null, 'user_id和to_user不可同时有值');
        }
        return $this->stepRun($data, 'transferCertiList', 'transferCertiList');
    }

    /*获取别人转让给我的证书列表（我未处理） 好像废弃了，与包含在上一个方法里面*/
    public function inHandCertificateList(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'page'=>1, 'listRows'=>15, 'type'=>' ']);
        return $this->stepRun($data, 'inHandCertificateList');
    }

    /*获取某个证书的转让记录列表 分页*/
    public function getTransferList(): array 
    {
        $data = $this->haveIssetReceiveData(['certificate_id'=>0, 'keywords'=>'', 'page'=>1, 'listRows'=>15]);
        return $this->stepRun($data, 'getTransferList');
    }


}